﻿using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Masuit.Tools.Security;
using AJWPFAdmin.Core.Properties;
using System.Diagnostics;
using AJWPFAdmin.Core.Models.EF.Tables;
using static AJWPFAdmin.Services.EF.TableExts.SystemDictionaryExt;
using AJWPFAdmin.Core.Enums;
using AJWPFAdmin.Core.Utils;
using AJWPFAdmin.Services.AppointAPI;
using DocumentFormat.OpenXml.Wordprocessing;
using Yitter.IdGenerator;
using AJWPFAdmin.Core.Interfaces;

namespace AJWPFAdmin.Services.EF
{
    public partial class DbService : DbContext, IDbServiceBridge
    {

        public DbService(DbContextOptions<DbService> options)
            : base(options)
        {
        }


        public DbSet<SystemUser> SystemUsers { get; set; }
        public DbSet<SystemRole> SystemRoles { get; set; }
        public DbSet<PlatformCustomer> PlatformCustomers { get; set; }
        public DbSet<ShippingRecord> ShippingRecords { get; set; }
        public DbSet<MaterialDictionary> MaterialDictionaries { get; set; }
        public DbSet<DatabaseBackupRecord> DatabaseBackupRecords { get; set; }
        public DbSet<Watchhouse> Watchhouses { get; set; }
        public DbSet<Passageway> Passageways { get; set; }
        public DbSet<Device> Devices { get; set; }
        public DbSet<CarType> CarTypes { get; set; }
        public DbSet<Car> Cars { get; set; }
        public DbSet<SystemConfigDictionary> SystemConfigDictionaries { get; set; }
        public DbSet<Warehouse> Warehouses { get; set; }
        public DbSet<WarehousePassageRecord> WarehousePassageRecords { get; set; }
        public DbSet<SystemDictionary> SystemDictionaries { get; set; }
        public DbSet<Goods> Goods { get; set; }

        public DbSet<WeighingDataCache> WeighingDataCaches { get; set; }

        public DbSet<EmiRiseRecord> EmiRiseRecords { get; set; }

        public DbSet<IllegalVehicleRecord> IllegalVehicleRecords { get; set; }

        public DbSet<ManualPassRecord> ManualPassRecords { get; set; }

        public DbSet<AlarmInfoRecord> AlarmInfoRecords { get; set; }

        public DbSet<BlacklistedCar> BlacklistedCars { get; set; }


        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                var version = new MySqlServerVersion(ServerVersion.Parse("8.0.34-mysql"));
                optionsBuilder.UseMySql("server=localhost;user=akira;password=mysql2014!@akira;database=shipping_system;AllowLoadLocalInfile=true", version, builder =>
                {
                    builder.EnableRetryOnFailure();
                }).EnableDetailedErrors();
                //.LogTo(log => Debug.WriteLine(log),
                //     new[] {
                //         DbLoggerCategory.Database.Command.Name,
                //         DbLoggerCategory.Update.Name,
                //         DbLoggerCategory.Query.Name
                //     }
                //    );
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            var now = DateTime.Parse("2024-11-26");

            #region 账户信息初始化

            modelBuilder.Entity<PlatformCustomer>()
                .HasData(new PlatformCustomer
                {
                    Id = 480306,
                    Name = "阿吉",
                    Director = "阿吉",
                    DirectorPhone = "15155555551",
                    Contact = "15155555551",
                    Address = "",
                    CreateDate = now,
                    ExpireDate = now.AddYears(1),
                    Enable = true
                });

            modelBuilder.Entity<SystemRole>()
                .HasData(new SystemRole
                {
                    Id = 480306,
                    PId = 480306,
                    Name = "管理员",
                    CreateDate = now,
                    UpdateDate = now,
                    Permission = "all"
                });

            modelBuilder.Entity<SystemUser>()
                .HasData(new SystemUser
                {
                    Id = 480306,
                    PId = 480306,
                    AccountName = "admin",
                    Phone = "15155555551",
                    Password = "123456".AESEncrypt(Resources.AESKey),
                    NickName = "admin",
                    Avatar = "",
                    Enable = true,
                    RoleId = 480306,
                    RoleName = "管理员",
                    CreateDate = now,
                    UpdateDate = now,
                });

            #endregion

            #region 车辆类型初始化

            modelBuilder.Entity<CarType>()
                .HasData(new CarType
                {
                    Id = (long)CarTypeEnum.进出厂车,
                    PId = 480306,
                    Name = CarTypeEnum.进出厂车.ToString(),
                    CreateDate = now,
                    SysRequired = true,
                });
            modelBuilder.Entity<CarType>()
                .HasData(new CarType
                {
                    Id = (long)CarTypeEnum.厂内运输车,
                    PId = 480306,
                    Name = CarTypeEnum.厂内运输车.ToString(),
                    CreateDate = now,
                    SysRequired = true
                });
            modelBuilder.Entity<CarType>()
                .HasData(new CarType
                {
                    Id = (long)CarTypeEnum.非道路移动机械,
                    PId = 480306,
                    Name = CarTypeEnum.非道路移动机械.ToString(),
                    CreateDate = now,
                    SysRequired = true,
                });

            modelBuilder.Entity<CarType>()
                .HasData(new CarType
                {
                    Id = (long)CarTypeEnum.固定车,
                    PId = 480306,
                    Name = CarTypeEnum.固定车.ToString(),
                    CreateDate = now,
                    SysRequired = true,
                });

            modelBuilder.Entity<CarType>()
                .HasData(new CarType
                {
                    Id = (long)CarTypeEnum.临时车,
                    PId = 480306,
                    Name = CarTypeEnum.临时车.ToString(),
                    CreateDate = now,
                    SysRequired = true,
                });

            modelBuilder.Entity<CarType>()
                .HasData(new CarType
                {
                    Id = (long)CarTypeEnum.免检车,
                    PId = 480306,
                    Name = CarTypeEnum.免检车.ToString(),
                    CreateDate = now,
                    SysRequired = true,
                });

            #endregion

            #region 第三方预约接口初始化

            modelBuilder.Entity<SystemConfigDictionary>()
                .HasData(new SystemConfigDictionary
                {
                    Key = SystemConfigKey.APIDefault,
                    BooleanValue = true,
                    StringValue = CommonUtil.AJSerializeObject(new DefaultAppointAPI.APIConfig
                    {
                        Url = string.Empty,
                        AppId = string.Empty
                    }),
                    CreateDate = now,
                });

            modelBuilder.Entity<SystemConfigDictionary>()
                .HasData(new SystemConfigDictionary
                {
                    Key = SystemConfigKey.APITangShan,
                    BooleanValue = false,
                    CreateDate = now,
                });

            modelBuilder.Entity<SystemConfigDictionary>()
                .HasData(new SystemConfigDictionary
                {
                    Key = SystemConfigKey.APILangFang,
                    BooleanValue = false,
                    CreateDate = now,
                });
            modelBuilder.Entity<SystemConfigDictionary>()
             .HasData(new SystemConfigDictionary
             {
                 Key = SystemConfigKey.APIQingDao,
                 BooleanValue = false,
                 CreateDate = now,
             });

            modelBuilder.Entity<SystemConfigDictionary>()
            .HasData(new SystemConfigDictionary
            {
                Key = SystemConfigKey.APIGanZhou,
                BooleanValue = false,
                CreateDate = now,
            });
            modelBuilder.Entity<SystemConfigDictionary>()
           .HasData(new SystemConfigDictionary
           {
               Key = SystemConfigKey.APILinYi,
               BooleanValue = false,
               CreateDate = now,
           });

            modelBuilder.Entity<SystemConfigDictionary>()
           .HasData(new SystemConfigDictionary
           {
               Key = SystemConfigKey.APIBinZhou,
               BooleanValue = false,
               CreateDate = now,
           });
            modelBuilder.Entity<SystemConfigDictionary>()
           .HasData(new SystemConfigDictionary
           {
               Key = SystemConfigKey.APIChengDu,
               BooleanValue = false,
               CreateDate = now,
           });

            modelBuilder.Entity<SystemConfigDictionary>()
                .HasData(new SystemConfigDictionary
                {
                    Key = SystemConfigKey.APIBeiJing,
                    BooleanValue = false,
                    CreateDate = now,
                });

            modelBuilder.Entity<SystemConfigDictionary>()
                .HasData(new SystemConfigDictionary
                {
                    Key = SystemConfigKey.APIQinYou,
                    BooleanValue = false,
                    CreateDate = now,
                });

            modelBuilder.Entity<SystemConfigDictionary>()
                .HasData(new SystemConfigDictionary
                {
                    Key = SystemConfigKey.APIDeYang,
                    BooleanValue = false,
                    CreateDate = now,
                });

            #endregion

            #region 7天试用初始化

            modelBuilder.Entity<SystemConfigDictionary>()
                .HasData(new SystemConfigDictionary
                {
                    Key = SystemConfigKey.TrialConfig,
                    StringValue = string.Empty,
                    CreateDate = now,
                });

            #endregion

            #region 字典根节点初始化

            modelBuilder.Entity<SystemDictionary>()
                .HasData(new SystemDictionary
                {
                    Id = (long)SystemDictionaryKey.货车,
                    Code = ((int)SystemDictionaryKey.货车).ToString(),
                    Name = SystemDictionaryKey.货车.ToString(),
                    ParentId = null,
                    CreateDate = now
                });

            modelBuilder.Entity<SystemDictionary>()
                .HasData(new SystemDictionary
                {
                    Id = (long)SystemDictionaryKey.燃料,
                    Code = ((int)SystemDictionaryKey.燃料).ToString(),
                    Name = SystemDictionaryKey.燃料.ToString(),
                    ParentId = null,
                    CreateDate = now
                });

            modelBuilder.Entity<SystemDictionary>()
                .HasData(new SystemDictionary
                {
                    Id = (long)SystemDictionaryKey.使用性质,
                    Code = ((int)SystemDictionaryKey.使用性质).ToString(),
                    Name = SystemDictionaryKey.使用性质.ToString(),
                    ParentId = null,
                    CreateDate = now
                });

            #endregion

        }
    }
}
